<?php
/**
 * 任务
 * 
 * Created	: 2010-10-11
 * Modified : 2010-10-15
 * @author  : 陈立强
 */
class Quest {
	public static $tbl = 'template_quest';
	
	/**
	 * 获取进行中的任务列表信息
	 * @param int $uid
	 */
	public static function getGoingList($uid)
	{
		global $DB, $L;
		// 获取当前玩家已进行中任务和已完成的任务
		$user = $DB->read('user', $uid, 'uid', 'going_quest,finished_quest');
		$going = $user['going_quest'];
		$where = 'tq_id in ('.$going.')';
		$questList = $DB->select(self::$tbl, '*', $where);
		return $questList;
	}
	
	/**
	 * 接任务
	 * @param int $uid
	 * @param int $tq_id 任务ID
	 * @return array
	 */
	public static function accept($uid, $tq_id)
	{
		global $DB, $L;
		// 获取当前玩家已进行中任务和已完成的任务
		$user = $DB->read('user', $uid, 'uid', 'going_quest,finished_quest');
		
		// 进行中的任务不可重复接受
		$going = ','.$user['going_quest'].',';
		if (false !== strpos($going, ','.$tq_id.','))
		{
			return array(
				'result' => false,
				'msg'    => $L['cannot_accept_going_quest']
			);
		}
		// 已完成且不可重复的任务
		$finished = ','.$user['finished_quest'].',';
		if (false !== strpos($finished, ','.$tq_id.','))
		{
			return array(
				'result' => false,
				'msg'    => $L['cannot_accept_going_quest']
			);
		}
		// 是否满足接受任务的其它条件
		;
		
		// 更新进行中任务数据
		if (empty($user['going_quest'])) {
			$user['going_quest'] = $tq_id;
		} else {
			$user['going_quest'] .= ','.$tq_id;
		}
		$data = array(
			'going_quest' => $user['going_quest']
		);
		$ret = $DB->update('user', $data, 'uid='.$uid);
		$result['val'] = $ret;
		if ($ret) {
			$result['msg'] = $L['Success'];
		} else {
			$result['msg'] = $L['Failure'];
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('quest_accept', $data);
		}
		return $result;
	}
	
	/**
	 * 完成任务
	 * @param int $tq_id
	 * @param int $tq_id 任务ID
	 * @return array
	 */
	public static function finish($uid, $tq_id)
	{
		global $DB, $L;
		// 获取当前玩家已进行中任务和已完成的任务
		$user = $DB->read('user', RID, 'uid', 'going_quest,finished_quest');
		
		// 进行中的任务才能完成
		$going = ','.$user['going_quest'].',';
		if (false === strpos($going, ','.$tq_id.','))
		{
			return array(
				'result' => false,
				'msg'    => $L['can_only_finished_going_quest']
			);
		}
		// 获取任务信息
		$questInfo = $DB->read('template_quest', $tq_id, 'tq_id');
		
		//- 判断是否满足完成任务的条件 
		;
		
		//-- 领取任务奖励
		;
		
		//-- 更新任务状态
		$going = explode(',', $user['going_quest']);
		if (empty($user['finished_quest'])) {
			$finished = $tq_id;
		} else {
			$finished = $user['finished_quest'].','.$tq_id;
		}
		// 任务从进行中列表删除
		$key = array_search($tq_id, $going);
		if (false !== $key) {
			unset($going[$key]);
		}
		
		$data = array(
			'going_quest' => join(',', $going),
			'finished_quest' => $finished,
		);
		$ret = $DB->update('user', $data, 'uid='.$uid);
		$result['val'] = $ret;
		if ($ret) {
			$result['msg'] = $L['Success'];
		} else {
			$result['msg'] = $L['Failure'];
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('quest_accept', $data);
		}
		return $result;
	}
	
	/**
	 * 取消/放弃任务
	 * @param int $uid
	 * @param int $tq_id 任务ID
	 * @return array
	 */
	public static function cancel($uid, $tq_id)
	{
		global $DB, $L;
		// 获取当前玩家已进行中任务和已完成的任务
		$user = $DB->read('user', $uid, 'uid', 'going_quest,finished_quest');
		
		// 无法取消不在进行中的任务
		$going = explode(',', $user['going_quest']);
		$key = array_search($tq_id, $going);
		if (false === $key)
		{
			return array(
				'result' => false,
				'msg'    => $L['cannot_accept_going_quest']
			);
		}
		
		//-- 取消任务
		unset($going[$key]);
		
		//-- 移除任务产生的一些专用物品?
		
		
		$DB->beginTransaction();
		
		$data = array(
			'going_quest' => join(',', $going),
		);
		$ret = $DB->update('user', $data, 'uid='.$uid);
		$result['val'] = $ret;
		if ($ret) {
			$DB->commit();
			$result['msg'] = $L['Success'];
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('quest_accept', $data);
		}
	}
	
	private static function removeQuestItem()
	{
		return true;
	}
}










